class IUP_TIMER
-- Creates a timer which periodically invokes a callback when the time is 
-- up. Each timer should be destroyed using "destroy".
-- In GTK uses g_timeout_add, in Windows uses SetTimer, and in Motif 
-- uses XtAppAddTimeOut.

inherit
	ANY
	IUP_WIDGET
		export
			{NONE} all
			{ANY} destroy
		redefine
			execute_action
		end

create {ANY}
   timer

feature {ANY}

	timer
		local
			a_timer: POINTER
		do
			a_timer := int_timer
			set_widget(a_timer)
		end

	set_time (time: INTEGER)
		-- The time interval in milliseconds. In Windows the minimum 
		-- value is 10ms.
		require
			time > 0
		do
			iup_open.set_attribute(Current, "TIME", time.out)
		end

	start
		do
			iup_open.set_attribute(Current, "RUN", "YES")
		end

	stop
		do
			iup_open.set_attribute(Current, "RUN", "NO")
		end

	is_running: BOOLEAN
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "RUN")

			if str.is_equal("YES") then
				Result := True
			else
				Result := False
			end
		end

	get_elapsed_time: INTEGER
		-- Inside the callback returns the time elapsed since
		-- the timer was started.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "ELAPSEDTIME")

			Result := str.to_integer
		end

	-- Callback

	set_cb_action (act: detachable FUNCTION[TUPLE[IUP_TIMER], STRING])
		-- Called every time the defined time interval is reached.
		-- To stop the callback from being called simply stop de timer. Inside 
		-- the callback "get_elapsed_time" returns the time elapsed since 
		-- the timer was started.
		--
		-- ih: element that activated the event.
		--
		-- Returns: "IUP_CLOSE" will be processed.
		local
			operation: INTEGER
		do
			cb_action := act

			if cb_action /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "ACTION_CB", "Fn", operation)
		end

feature {ANY}

	execute_action: STRING
		do
			if attached cb_action as int_cb then
				Result := int_cb.item([Current])
			else
				Result := "IUP_DEFAULT"
			end
		end

feature {NONE}

	cb_action: detachable FUNCTION[TUPLE[IUP_TIMER], STRING]

	-- Internal
	
	int_timer: POINTER
		external
			"C inline use %"eiffel-iup.h%""
      alias
			"return IupTimer();"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2019, 2020 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
